package com.lzh.commonutils;

import org.springframework.util.DigestUtils;


public class Md5Util {

    /**
     * MD5加密
     * @param plainText
     * @return
     */
    public static String toMD5(String plainText) {
        return DigestUtils.md5DigestAsHex(plainText.getBytes());
    }

    /**
     * MD5加盐
     */
    public static String toMD5(String plainText,String salt){
        return DigestUtils.md5DigestAsHex((plainText+salt).getBytes());
    }

    public static void main(String[] args) {
        String plainText = "c95670b28fadb31092b23cee1bf02af8";
        //MD5加密
        String encode = toMD5(plainText);
        System.out.println("未加盐："+encode);

        //MD5加盐加密
        encode = toMD5(plainText,"heimatoutiao");
        System.out.println("已加盐："+encode);

        //动态加盐
        String salt = RandomUtil.random(10);
        encode = toMD5(plainText,salt);
        System.out.println("动态加盐："+encode);
    }
}
